import axios from 'axios'; // 引入axios
// 下面这两个不一定需要引入，看你项目需要拦截的时候做什么操作，但是一般都需要引入store
// import store from '@/store/index' // 引入store
// import router from '@/router' // 引入router

const service = axios.create({
  headers: {
    'content-type': 'application/json',
  },
});

// http service 拦截器
service.interceptors.request.use(
  (config) => {
    // const token = sessionStorage.getItem('token')
    // if (token) { // 判断是否存在token，如果存在的话，则每个http header都加上token
    //   config.headers.authorization = token // 请求头加上token
    // }
    console.log(config);
  },
  (err) => Promise.reject(err),
);

// http response 拦截器
service.interceptors.response.use(
  (response) => {
    // 拦截响应，做统一处理
    // if (response.data.code) {
    //   switch (response.data.code) {
    //     case 1002:
    //       store.state.isLogin = false
    //       router.replace({
    //         path: 'login',
    //         query: {
    //           redirect: router.currentRoute.fullPath
    //         }
    //       })
    //   }
    // }
    console.log(response);
  },
  // 接口错误状态处理，也就是说无响应时的处理
  (error) => Promise.reject(error.response), // 返回接口返回的错误信息

);

const localAxios = {
  getUrl(url, config = {}, isLoading = true) {
    return new Promise((resolve, reject) => {
      service.get(url, { ...config, __isLoading__: isLoading })
        .then((response) => {
          resolve(response);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },
  getUrlCallBack(url, callback, config = {}, isLoading = true) {
    return new Promise(() => {
      service.get(url, { ...config, __isLoading__: isLoading })
        .then((response) => {
          callback(response);
        })
        .catch((err) => {
          console.error(`getUrlCallBack:${url}`, err);
        });
    });
  },
  postUrl(url, paramObj, config = {}, isLoading = true) {
    return new Promise((resolve, reject) => {
      service.post(url, paramObj, { ...config, __isLoading__: isLoading })
        .then((response) => {
          resolve(response);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },
  postUrlCallBack(url, paramObj, callback, config = {}, isLoading = true) {
    return new Promise(() => {
      service.post(url, paramObj, { ...config, __isLoading__: isLoading })
        .then((response) => {
          callback(response);
        })
        .catch((err) => {
          console.error(`postUrlCallBack:${url}`, paramObj, err);
        });
    });
  },
  putUrl(url, paramObj, config = {}, isLoading = true) {
    return new Promise((resolve, reject) => {
      service.put(url, paramObj, { ...config, __isLoading__: isLoading })
        .then((response) => {
          resolve(response);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },
  putUrlCallBack(url, paramObj, callback, config = {}, isLoading = true) {
    return new Promise(() => {
      service.put(url, paramObj, { ...config, __isLoading__: isLoading })
        .then((response) => {
          callback(response);
        })
        .catch((err) => {
          console.error(`putUrlCallBack:${url}`, paramObj, err);
        });
    });
  },
  patchUrl(url, paramObj, config = {}, isLoading = true) {
    return new Promise((resolve, reject) => {
      service.patch(url, paramObj, { ...config, __isLoading__: isLoading })
        .then((response) => {
          resolve(response);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },
  patchUrlCallBack(url, paramObj, callback, config = {}, isLoading = true) {
    return new Promise(() => {
      service.patch(url, paramObj, { ...config, __isLoading__: isLoading })
        .then((response) => {
          callback(response);
        })
        .catch((err) => {
          console.error(`patchUrlCallBack:${url}`, paramObj, err);
        });
    });
  },
  // 批量请求示例
  // service.all([
  //   axios.get('/api/service1'),
  //   axios.get('/api/service2'),
  //   axios.get('/api/service3'),
  //   axios.get('/api/service4')])
  //   .then(service.spread((res1,res2,res3,res4)={

  //   })),
  // connectSSE(url) {
  //   if (window.EventSource) {
  //     const source = new EventSource(url);
  //     let reconnectTimeout;

  //     source.addEventListener('open', () => {
  //       console.log('Connected');
  //       clearTimeout(reconnectTimeout);
  //     }, false);
  //     // 服务器发送信息到客户端时，如果没有 event 字段，默认会触发 message 事件
  //     source.addEventListener('message', (e) => {
  //       console.log(`data: ${e.data}`);
  //     }, false);
  //     // 连接异常时会触发 error 事件并自动重连
  //     source.addEventListener('error', (e) => {
  //       if (e.target.readyState === EventSource.CLOSED) {
  //         console.log('Disconnected');
  //       } else if (e.target.readyState === EventSource.CONNECTING) {
  //         console.log('Connecting...');
  //       }
  //     }, false);
  //     source.addEventListener('pause', (e) => {
  //       source.close();
  //       const reconnectTime = +e.data;
  //       const currentTime = +new Date();
  //       reconnectTimeout = setTimeout(() => {
  //         connectSSE();
  //       }, reconnectTime - currentTime);
  //     }, false);
  //     return source;
  //   }
  //   console.error('Your browser doesn\'t support SSE');
  // },

};
export default localAxios;
